package com.zhike.b2b.html;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhike.b2b.base.BaseAction;
import com.zhike.b2b.model.ClassVo;
import com.zhike.b2b.util.ClassUtil;

@Controller
public class HtmlAction extends BaseAction {
	
	@RequestMapping("/html/connect.do")
	public String connect(HttpServletRequest request)
	{
		String url = (String)request.getAttribute("javax.servlet.error.message");  //.getHeader("javax.servlet.error.message");
		boolean flag = false;
		int inx = url.lastIndexOf("/");
		int linx = url.indexOf(".html");
		if(linx<0)
			return "miss";
		try {
			String fileName = url.substring(inx+1,linx);
			String[] words = fileName.split("-");
			ServletContext ctx = request.getSession().getServletContext();
			if(url.indexOf("product")>=0)
			{
				if(url.indexOf("content")>=0)
				{
					//获取产品(供应)Id
					String prodId = words[2];
					flag = prs.createContentHtml(prodId, ctx);
				}else if(url.indexOf("list")>=0){
					//获取产品(供应)类别Id
					String classId = words[1];
					int pageNo = Integer.parseInt(words[2]);
					flag = prs.createListHtml(classId, pageNo, ctx);
				}
			}else if(url.indexOf("buy")>=0)
			{
				if(url.indexOf("content")>=0)
				{
					//获取求购信息Id
					String buyId = words[2];
					flag = is.createContentHtml(buyId, ctx);
				}else if(url.indexOf("list")>=0){
					//获取求购信息类别Id
					String classId = words[1];
					int pageNo = Integer.parseInt(words[2]);
					flag = is.createListHtml(classId, pageNo, ctx);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		if(!flag)
			return "redirect:/connect.html";
		else
			return "redirect:"+url;
	}
	
	@RequestMapping("/admin/createhtml.do")
	public String createHtml(@RequestParam("type") String type,@RequestParam("classId") String classId,
			@RequestParam("createType") String createType,HttpServletRequest request,Model model)
	{
		ServletContext ctx = request.getSession().getServletContext();
		boolean flag = false;
		try {
			if("prod".equals(type))
			{
				if("2".equals(createType))
				{
				   model.addAttribute("url","/product/index.html");
				   flag = prs.createIndexHtml(ctx);
				}else if("3".equals(createType))
				{
				   ClassVo cv = pcs.getClassById(classId);
				   String lurl = ClassUtil.getListUrl(cv, 1);
				   model.addAttribute("url",lurl); 
				   flag = prs.createListHtml(classId, 1, ctx);
				}else if("4".equals(createType))
				{
					
				}
			}else if("buy".equals(type))
			{
				if("2".equals(createType))
				{
				   model.addAttribute("url","/buying/index.html");
				   flag = is.createIndexHtml(ctx);
				}else if("3".equals(createType))
				{
				   ClassVo cv = is.getClassById(classId);
				   String lurl = ClassUtil.getListUrl(cv, 1);
				   model.addAttribute("url",lurl); 
				   flag = is.createListHtml(classId, 1, ctx);
				}
			}else if("news".equals(type))
			{
				
			}else if("expo".equals(type))
			{
				
			}else if("com".equals(type))
			{
				
			}else if("job".equals(type))
			{
				
			}else if("apply".equals(type))
			{
				
			}else if("serv".equals(type))
			{
				
			}else{
				model.addAttribute("url","/index.html");
			}
			model.addAttribute("createType", createType);
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return "/admin/html/result";
	}
	
	@RequestMapping("/admin/clzs.do")
	public String classInfo(@RequestParam("parent") String parent,@RequestParam("type") String type,Model model)
	{
		List<ClassVo> lcv = null;
		if("root".equals(type))
		{
			lcv = hs.getRootModels();
		}else{
			String clzTblName = "";
			if("prod".equals(type))
				clzTblName = "product_class";
			else if("buy".equals(type))
				clzTblName = "buying_class";
			else if("news".equals(type))
				clzTblName = "news_class";
			else if("expo".equals(type))
				clzTblName = "expo_class";
			else if("com".equals(type))
				clzTblName = "company_class";
			else if("job".equals(type))
				clzTblName = "jobs_class";
			else if("apply".equals(type))
				clzTblName = "work_class";
			else if("serv".equals(type))
				clzTblName = "service_class";
			lcv = hs.getClassInfoByParent(clzTblName, parent, type);
		}
		model.addAttribute("lcv", lcv);
		return "/admin/html/class_info";
	}
}
